*****************************************************************************************************************************
**************** REPICATION DO-FILE FOR Andersson & Teorell, "The Double-Edged Sword", country-leader-year data *************
*****************************************************************************************************************************

use "autocratic survival data.dta", clear

* FIGURE 1
* Collapse into country year data
preserve
  collapse state e_boix_regime country_id (max) fail_irreg=fail_irreg /* if state<.*/,by(country_name year)
  gen onlydem=1-e_boix_regime
  by country_id, sort: egen tonlydem=total(onlydem) /*if state<.*/
  drop if tonlydem==0
  by country_id, sort: egen nstate=count(state)
  drop if nstate==0
  drop if country_name==""
  xtset country_id year
  tsfill, full
  replace country_name=country_name[_n-1] if country_name=="" & country_id==country_id[_n-1]
  gen dem=e_boix_regime==1 & (l.e_boix_regime==0 | l.e_boix_regime==.) 
  gen dem2=dem if l5.state<.
  gen state2=state
  replace state2=. if e_boix_regime==1
  drop if country_name=="Costa Rica" | country_name=="Dominican Republic" | country_name=="Poland" | country_name=="Nicaragua" /* Because all zero on dem2 */
  twoway (line state2 year,yaxis(1) cmissing(n)) (spike fail_irreg year if state2<. & e_boix_regime==0,lcolor(maroon) yaxis(2)) (spike dem2 year,yaxis(2) lwidth(thick) lcolor(forest_green)) ,by(country_name) legend(order(1 "State capacity" 2 "Irregular removal" 3 "Democratization") col(1) size(small)) ytitle("State capacity", size(small)) xtitle("") ylab(0 10 20,axis(1)) ylab(0 1,axis(2)) scheme(lean2) name(Datagraph, replace)
  * NOTE: To produce the figure as it appears in the paper, we had to manually empty the "Note" (Graphs by country name) and move the legend 
restore

* TABLE 1
label var fail_irreg "Irregular removal"
label var state "State capacity"
label var lstate "State capacity at t-1"
label var leg "Legislature"
label var lleg "Legislature at t-1"
label var party "Partisan leader"
label var growth "Economic growth"
label var lgrowth "Economic growth at t-1"
label var fariss_lnpop "Ln(population)"
label var lfariss_lnpop "Ln(population) at t-1"
label var fariss_lngdppc "Ln(GDP/capita)"
label var lfariss_lngdppc "Ln(GDP/capita) at t-1"
label var lnoil "Ln(Petroleum production/capita)"
label var llnoil "Ln(Petroleum production/capita) at t-1"
label var e_miinterc "Internal armed conflict"
label var le_miinterc "Internal armed conflict at t-1"
label var e_miinteco "International armed conflict"
label var le_miinteco "International armed conflict at t-1"
label var cold "Cold war"
label var age "Leader age"
label var v2ex_hosw "Relative power of leader"
label var lnmil "Ln(military personnel/capita)"
label var llnmil "Ln(military personnel/capita) at t-1"
label var t "Time in office"
label var t2 "Time in office^2"
label var t3 "Time in office^3"
eststo clear
set more off
* Model 1
logit fail_irreg lstate llnmil lleg party lgrowth lfariss_lnpop lfariss_lngdppc llnoil le_miinterc le_miinteco cold age v2ex_hosw t t2 t3 if e_boix_regime==0,cluster(leadspell_id)
preserve
  collapse state if e(sample),by(country_id)
  qui sum state
  local N_sub=`r(N)'
restore
qui sum state if _d==1 & e(sample)
local N_fail=`r(N)'
eststo m1new, addscalar(N_sub `N_sub' N_fail `N_fail')
* Model 2
logit fail_irreg c.lstate##c.llnmil lleg party lgrowth lfariss_lnpop lfariss_lngdppc llnoil le_miinterc le_miinteco cold age v2ex_hosw t t2 t3 if e_boix_regime==0,cluster(leadspell_id)
preserve
  collapse state if e(sample),by(country_id)
  qui sum state
  local N_sub=`r(N)'
restore
qui sum state if _d==1 & e(sample)
local N_fail=`r(N)'
eststo m2new, addscalar(N_sub `N_sub' N_fail `N_fail')
* Model 3
logit fail_irreg lstate llnmil lleg party lgrowth lfariss_lnpop lfariss_lngdppc llnoil le_miinterc le_miinteco cold age v2ex_hosw t t2 t3 if e_boix_regime==0 & !(Officetype==1 | Officetype==3 | Officetype==8),cluster(leadspell_id)
preserve
  collapse state if e(sample),by(country_id)
  qui sum state
  local N_sub=`r(N)'
restore
qui sum state if _d==1 & e(sample)
local N_fail=`r(N)'
eststo m3new, addscalar(N_sub `N_sub' N_fail `N_fail')
* Model 4
logit fail_irreg c.lstate##c.llnmil lleg party lgrowth lfariss_lnpop lfariss_lngdppc llnoil le_miinterc le_miinteco cold age v2ex_hosw t t2 t3 if e_boix_regime==0 & !(Officetype==1 | Officetype==3 | Officetype==8),cluster(leadspell_id)
preserve
  collapse state if e(sample),by(country_id)
  qui sum state
  local N_sub=`r(N)'
restore
qui sum state if _d==1 & e(sample)
local N_fail=`r(N)'
eststo m4new, addscalar(N_sub `N_sub' N_fail `N_fail')

esttab, b(3) se(3) star(* 0.10 ** 0.05 *** 0.01 ) order(lstate llnmil c.lstate#c.llnmil lleg lgrowth lfariss_lnpop lfariss_lngdppc llnoil le_miinterc le_miinteco cold party age v2ex_hosw) sca("N" "N_clust" "N_sub" "N_fail") replace obslast label nogaps /* eform */ mtitles("All leaders" "All leaders" "De facto leaders" "De facto leaders")
* NOTE: To produce the table as it appears in the paper, the text explaining the content of the four last rows has been inserted, and the trailing zeros deleted, manually

* FIGURE 2
qui logit fail_irreg c.lstate##c.llnmil lleg party lgrowth lfariss_lnpop lfariss_lngdppc llnoil le_miinterc le_miinteco cold age v2ex_hosw t t2 t3 if e_boix_regime==0,cluster(leadspell_id)
gen pipe = "|"
gen rugplot=-0.03
margins, dydx(lstate) at(llnmil==(0(1)10))
marginsplot,  plot1opts(recast(line) lwidth(.8) lpattern(dash_dot)) ci1opts(recast(rline) xlabel(#5) lcolor(gs10) lpattern(dash_dot)) ytitle("Effect of SC at t-1 on p(irregular removal)") xtitle ("Ln(military personnel/capita) at t-1") title("") scheme(lean2) addplot(scatter rugplot llnmil if llnmil>0 & llnmil<8, ms(none) ml(pipe)) leg(off) name(graph_irregular_interaction)
* NOTE: To produce the figure as it appears in the paper, we manually moved the y-title closer to the y-axis 

* APPENDIX A. TABLE A1
eststo clear
qui logit fail_irreg lstate llnmil lleg party lgrowth lfariss_lnpop lfariss_lngdppc llnoil le_miinterc le_miinteco cold age v2ex_hosw t t2 t3 if e_boix_regime==0,cluster(leadspell_id)
qui estpost sum  fail_irreg lstate llnmil lleg lgrowth lfariss_lnpop lfariss_lngdppc llnoil le_miinterc le_miinteco cold party age v2ex_hosw if e(sample)
esttab, replace nomtitles collabels("Mean" "Std.Dev." "Min" "Max" "Obs") cells("mean(fmt(3)) sd(fmt(3)) min(fmt(3)) max(fmt(3)) count(fmt(0))") label nonumber noobs varwidth(32)

* APPENDIX B. TABLE B1
eststo clear
stcox state lnmil leg party growth fariss_lnpop fariss_lngdppc lnoil e_miinterc e_miinteco cold age v2ex_hosw if e_boix_regime==0,/*cluster(leadspell_id)*/ shared(country_id) forceshared
eststo m1
stcox c.state##c.lnmil leg party growth fariss_lnpop fariss_lngdppc lnoil e_miinterc e_miinteco cold age v2ex_hosw if e_boix_regime==0,/*cluster(leadspell_id)*/ shared(country_id) forceshared
eststo m2
stcox state lnmil leg party growth fariss_lnpop fariss_lngdppc lnoil e_miinterc e_miinteco cold age v2ex_hosw if e_boix_regime==0 & !(Officetype==1 | Officetype==3 | Officetype==8), shared(country_id) forceshared
eststo m3
stcox c.state##c.lnmil leg party growth fariss_lnpop fariss_lngdppc lnoil e_miinterc e_miinteco cold age v2ex_hosw if e_boix_regime==0 & !(Officetype==1 | Officetype==3 | Officetype==8), shared(country_id) forceshared
eststo m4
esttab, b(3) se(3) star(* 0.10 ** 0.05 *** 0.01 ) order(state lnmil c.state#c.lnmil) sca("N" "N_g" "N_sub" "N_fail") replace obslast label nogaps eform mtitles("All leaders" "All leaders" "De facto leaders" "De facto leaders")
* NOTE: To produce the table as it appears in the paper, the text explaining the content of the four last rows has been inserted, and the trailing zeros deleted, manually


